package weekMatch.one_102;

import javax.swing.table.TableModel;
import java.util.Arrays;

public class LeeCode_03_failed {
    public static void main(String[] args) {
//        System.out.println(miceAndCheese(new int[]{1, 1, 3, 4}, new int[]{4, 4, 1, 1}, 2));
//        System.out.println(miceAndCheese(new int[]{1,1}, new int[]{1,1}, 2));
//        System.out.println(miceAndCheese(new int[]{4,1,5,3,3}, new int[]{3,4,4,5,2}, 3));
//        System.out.println(miceAndCheese(new int[]{1,2,2,2,1,1}, new int[]{1,2,1,1,1,1}, 2));
//        System.out.println(miceAndCheese(new int[]{4,1,4,3,2,3}, new int[]{4,4,4,1,4,1}, 3));
        System.out.println(miceAndCheese(new int[]{1,1,4,1,2,3}, new int[]{1,2,4,1,3,3}, 4));
        System.out.println(miceAndCheese(new int[]{1}, new int[]{4}, 1));
    }
    private static int miceAndCheese(int[] reward1, int[] reward2, int k) {
        int len = reward1.length;
        quickSort(reward1, 0, len - 1, reward2);
        int m = reward1.length - 1;
        while (m > 0){
            int num = reward1[m];
            int end = m;
            while (end > 0 && reward1[end] == num){
                end--;
            }
            end++;
            if (m == end){
                m--;
                continue;
            }
            Arrays.sort(reward2, end, m + 1);
            int start = m;
            m = end - 1;
            while (end < start){
                int temp = reward2[start];
                reward2[start] = reward2[end];
                reward2[end] = temp;
                start--;
                end++;
            }
        }
        int res1 = 0;
        for (int i = len - k; i < len; i++) {
            res1 += reward1[i];
        }
        for (int i = 0; i < len - k; i++) {
            res1 += reward2[i];
        }

        quickSort(reward2, 0, len - 1, reward1);
        m = reward1.length - 1;
        while (m > 0){
            int num = reward2[m];
            int end = m;
            while (end > 0 && reward2[end] == num){
                end--;
            }
            end++;
            if (m == end){
                m--;
                continue;
            }
            Arrays.sort(reward1, end, m + 1);
            int start = m;
            m = end - 1;
            while (end < start){
                int temp = reward1[start];
                reward1[start] = reward1[end];
                reward1[end] = temp;
                start--;
                end++;
            }
        }
        int res2 = 0;
        for (int i = 0; i < k; i++) {
            res2 += reward1[i];
        }
        for (int i = k; i < len; i++) {
            res2 += reward2[i];
        }
        return Math.max(res1, res2);
    }
    private static void quickSort(int[] nums, int i, int j, int[] nums2){
        if (i >= j)
            return;
        int start = i;
        int end = j;
        int baseNum = nums[i];
        int baseNum2 = nums2[i];
        while (start != end){
            while (true){
                if (start >= end || nums[end] < baseNum)
                    break;
                end--;
            }
            while (true){
                if (start >= end || nums[start] > baseNum) {
                    break;
                }
                start++;
            }
            int temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;

            int temp2 = nums2[start];
            nums2[start] = nums2[end];
            nums2[end] = temp2;
        }
        nums[i] = nums[start];
        nums[start] = baseNum;

        nums2[i] = nums2[start];
        nums2[start] = baseNum2;

        quickSort(nums, i, start - 1, nums2);
        quickSort(nums, start + 1, j, nums2);
    }
}
